<?php

/*
 * .
 *
 * 
 */

namespace App\Http\Controllers\Backend;

use App\Services\Business\Models\Activity;
use App\Services\Work\Models\Chat;
use App\Services\Work\Models\Staff;
use App\Services\Work\Interfaces\ChatServiceInterface;
use App\Services\Work\Services\ChatService;
use Carbon\Carbon;
use Illuminate\Http\Request;


class ChatController extends BaseController
{
    /**
     * @var ChatService
     */
    protected $chatService;

    public function __construct(
        ChatServiceInterface $chatService
    )
    {
        $this->chatService = $chatService;

    }



    public function customers(Request $request)
    {

        $id = $request->input('id');
        $corpId = $request->input('corp_id', 36);
        $chatId = $request->input('chat_uuid', '');
        $keywords = $request->input('keywords', '');
        $size = $request->input('size', 10);
        ['teachers' => $teachers, 'customers' => $customers] = $this->chatService->customers($corpId, $id, $chatId, $keywords, $size);
        return $this->successData(compact('teachers', 'customers'));


    }

    public function index(Request $request)
    {

        $corpId = $request->input('corp_id',36);
        $keywords = $request->input('keywords', '');
        $departmentId = $request->input('departmentId', 0);
        $userIds[] = $request->input('userId', '');
        if (!empty($departmentId)) {
            if ($departmentId == 1) {
                $userIds = Staff::query()
                    ->when($corpId, function ($query) use ($corpId) {
                        return $query->where('corp_id', $corpId);
                    })
                    ->get()
                    ->pluck('user_id');

            } else {
                $userIds = Staff::query()
                    ->when($departmentId, function ($query) use ($departmentId) {
                        return $query->whereRaw("FIND_IN_SET({$departmentId},`department_id`)");
                    })
                    ->when($corpId, function ($query) use ($corpId) {
                        return $query->where('corp_id', $corpId);
                    })
                    ->get()
                    ->pluck('user_id');
            }

        }
        $groups = Chat::query()
            ->where('corp_id', $corpId)
            ->when($keywords, function ($query) use ($keywords) {
                return $query->where('name', 'like', '%' . $keywords . '%');
            })
            ->when($userIds, function ($query) use ($userIds) {
                return $query->whereIn('owner_user_id', $userIds);
            })
            ->orderBy('created_at')
            ->paginate($request->input('size', 10));

        return $this->successData(compact('groups'));


    }

    public function detail($id)
    {
        $group = Chat::query()
            ->with(['prizes'])
            ->where('id', $id)
            ->firstOrFail();
        return $this->successData(compact("group"));
    }


    public function edit($id)
    {
        $group = Chat::query()->with(['prizes'])->where('id', $id)->firstOrFail();

        return $this->successData(compact('group'));
    }


    public function activities(Request $request, $id)
    {
        $page = $request->input('page', 1);
        $size = $request->input('size', 20);
        $activities = Activity::query()
            ->with(['user'])
            ->orderByDesc('created_at')
            ->forPage($page, $size)
            ->get()
            ->groupBy(function ($activity) {
                return Carbon::parse($activity->created_at)->format('Y-m-d');
            });

        return $this->successData(compact('activities'));


    }

    public function bindPrize(Request $request, $id)
    {

        $data = $request->input('prizes', []);
        $data = collect($data)->keyBy('id')->map(function ($value) {
            return collect($value)->except('id');
        });
        $group = Chat::find($id);
        $group->prizes()->attach($data);
        return $this->successData(__('绑定奖品成功'));


    }

    public function unbindPrize(Request $request, $id)
    {

        $ids = $request->input('ids', []);
        $group = Chat::find($id);
        $group->prizes()->detach($ids);
        return $this->successData(__('取消绑定奖品成功'));


    }



}
